package com.tianji.learning.service.impl;

import cn.hutool.core.lang.hash.Hash;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianji.api.cache.CategoryCache;
import com.tianji.api.client.course.CatalogueClient;
import com.tianji.api.client.course.CourseClient;
import com.tianji.api.client.search.SearchClient;
import com.tianji.api.client.user.UserClient;
import com.tianji.api.dto.course.CataSimpleInfoDTO;
import com.tianji.api.dto.course.CourseFullInfoDTO;
import com.tianji.api.dto.course.CourseSimpleInfoDTO;
import com.tianji.api.dto.user.UserDTO;
import com.tianji.common.domain.dto.PageDTO;
import com.tianji.common.exceptions.BadRequestException;
import com.tianji.common.exceptions.BizIllegalException;
import com.tianji.common.utils.BeanUtils;
import com.tianji.common.utils.ObjectUtils;
import com.tianji.common.utils.UserContext;
import com.tianji.learning.domain.dto.QuestionFormDTO;
import com.tianji.learning.domain.po.InteractionQuestion;
import com.tianji.learning.domain.po.InteractionReply;
import com.tianji.learning.domain.po.QuestionAdminPageQuery;
import com.tianji.learning.domain.vo.QuestionAdminVO;
import com.tianji.learning.domain.vo.QuestionVO;
import com.tianji.learning.mapper.InteractionQuestionMapper;
import com.tianji.learning.mapper.InteractionReplyMapper;
import com.tianji.learning.query.QuestionPageQuery;
import com.tianji.learning.service.IInteractionQuestionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 互动提问的问题表 服务实现类
 * </p>
 *
 * @author wangyuqing
 * @since 2024-08-25
 */
@Service
@RequiredArgsConstructor
public class InteractionQuestionServiceImpl extends ServiceImpl<InteractionQuestionMapper, InteractionQuestion> implements IInteractionQuestionService {
    private final InteractionReplyMapper replyMapper;
    private final UserClient userClient;
    private final SearchClient searchClient;
    private final CourseClient courseClient;
    private final CatalogueClient catalogueClient;
    private final CategoryCache categoryCache;


    /**
     * 1. 新增互动提问
     *
     * @param questionFormDTO
     */
    @Override
    public void addInteractionQuestion(QuestionFormDTO questionFormDTO) {
        //1.将Dto转换为po
        InteractionQuestion question = BeanUtils.copyBean(questionFormDTO, InteractionQuestion.class);
        //2.填补缺失字段
        question.setUserId(UserContext.getUser());
        //3.保存到数据库
        save(question);
    }

    /**
     * 2.根据ID查询问题详情
     *
     * @param id 问题ID
     */
    @Override
    public QuestionVO queryQuestionById(Long id) {
        //1.根据ID查询问题,问题状态为不显示的不予返回
        //select * from interaction_question where id = ? and hidden = 0
        InteractionQuestion question = getById(id);
        if (ObjectUtils.isEmpty(question) || question.getHidden()) {
            return null;
        }
        //2.调用服务，查询用户信息
        UserDTO user = null;
        QuestionVO questionVO = BeanUtils.toBean(question, QuestionVO.class);
        if (!question.getAnonymity()) {
            //2.1、用户没有设置为匿名，进行查询用户信息
            user = userClient.queryUserById(question.getUserId());
            //3.组装数据返回
            if (ObjectUtils.isNotEmpty(user)) {
                questionVO.setUserName(user.getName());
                questionVO.setUserIcon(user.getIcon());
            }
        }
        return questionVO;
    }


    /**
     * 3.分页查询问题详情
     *
     * @param pageQuery 分页查询参数对象
     */
    @Override
    public PageDTO<QuestionVO> queryQuestionPage(QuestionPageQuery pageQuery) {
        //1.查询问题数据,查询条件参数校验，课程ID和小节ID，必须同时拥有
        Long courseId = pageQuery.getCourseId();
        Long sectionId = pageQuery.getSectionId();
        if (ObjectUtils.isEmpty(courseId) && ObjectUtils.isEmpty(sectionId)) {
            throw new BizIllegalException("课程ID和小节ID必须同时存在");
        }
        //2.调用服务，查询问题信息
        Page<InteractionQuestion> page = lambdaQuery()
                .select(InteractionQuestion.class, info -> !info.getProperty().equals("description"))
                .eq(courseId != null, InteractionQuestion::getCourseId, courseId)
                .eq(sectionId != null, InteractionQuestion::getSectionId, sectionId)
                .eq(pageQuery.getOnlyMine(), InteractionQuestion::getUserId, UserContext.getUser())
                .eq(InteractionQuestion::getHidden, false)
                .page(pageQuery.toMpPageDefaultSortByCreateTimeDesc());
        List<InteractionQuestion> records = page.getRecords();
        if (ObjectUtils.isEmpty(records)) {
            return PageDTO.empty(page);
        }
        // 3、补充信息查询，查询提问者信息（非匿名的）、查询最近一次回答信息
        // 3.1、收集提问者ID集合，最近一次回答ID集合
        Set<Long> userIds = new HashSet<>();
        Set<Long> lastAnswerIds = new HashSet<>();
        for (InteractionQuestion record : records) {
            if (!record.getAnonymity()) {
                userIds.add(record.getUserId());
            }
            lastAnswerIds.add(record.getLatestAnswerId());
        }
        // 3.2、根据最近一次回答ID集合进行查询 -> 转成Map（最近一次回答ID ： 最近一次回答信息对象）
        Map<Long, InteractionReply> replyMap = new HashMap<>(lastAnswerIds.size());
        List<InteractionReply> lastReplyList = replyMapper.selectBatchIds(lastAnswerIds);
        if (!ObjectUtils.isEmpty(lastReplyList)) {
            for (InteractionReply reply : lastReplyList) {
                replyMap.put(reply.getId(), reply);
                if (!reply.getAnonymity()) {
                    userIds.add(reply.getUserId());
                }
            }
        }
        // 3.3、 根据提问者ID集合远程调用用户服务，查询用户信息 -> 转成Map（用户ID：用户信息对象）
        Map<Long, UserDTO> userInfoMap = new HashMap<>(userIds.size());
        if (ObjectUtils.isNotEmpty(userIds)) {
            List<UserDTO> userInfoList = userClient.queryUserByIds(userIds);
            if (ObjectUtils.isNotEmpty(userInfoList)) {
                userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserDTO::getId, Function.identity()));
            }
        }
        // 4、循环遍历分页数据，封装VO集合
        List<QuestionVO> voList = new ArrayList<>(records.size());
        for (InteractionQuestion record : records) {
            // 4.1、将po转为vo
            QuestionVO vo = BeanUtils.copyBean(record, QuestionVO.class);

            // 4.2、补充提问者信息（提问者昵称、头像）
            if (!record.getAnonymity()) {
                UserDTO userInfo = userInfoMap.get(record.getUserId());
                if (ObjectUtils.isNotEmpty(userInfo)) {
                    vo.setUserName(userInfo.getName());
                    vo.setUserIcon(userInfo.getIcon());
                }
            }
            // 4.3、补充最近一次回答信息（最近一次回答者昵称、最近一次回答内容）
            InteractionReply lastReply = replyMap.get(record.getLatestAnswerId());
            if (ObjectUtils.isNotEmpty(lastReply)) {
                vo.setLatestReplyContent(lastReply.getContent());
                if (!lastReply.getAnonymity()) {
                    UserDTO userInfo = userInfoMap.get(lastReply.getUserId());
                    if (ObjectUtils.isNotEmpty(userInfo)) {
                        vo.setLatestReplyUser(userInfo.getName());
                    }
                }
            }

            // 4.4、将vo添加至volist
            voList.add(vo);
        }

        // 5、组装PageDTO并返回
        return PageDTO.of(page, voList);
    }


    /**
     * 4、管理端-分页查询互动问题（Day5 - 随堂4）
     *
     * @param query
     * @return
     */
    @Override
    public PageDTO<QuestionAdminVO> adminPageQuery(QuestionAdminPageQuery query) {
        // 1、调用ES处理课程名称，得到课程ID集合
        List<Long> courseIds = null;
        if (ObjectUtils.isNotEmpty(query.getCourseName())) {
            courseIds = searchClient.queryCoursesIdByName(query.getCourseName());
            if (ObjectUtils.isEmpty(courseIds)) {
                return PageDTO.empty(0L, 0L);
            }
        }

        // 2、分页查询问题列表
        // select * from interaction_question where course_id in (1,2,3)
        // and status = ? and create_time >= ? and  create_time <= ? order by create_time desc limit ?,?
        Page<InteractionQuestion> pageResult = this.lambdaQuery()
                .in(ObjectUtils.isNotEmpty(courseIds), InteractionQuestion::getCourseId, courseIds)
                .eq(ObjectUtils.isNotEmpty(query.getStatus()), InteractionQuestion::getStatus, query.getStatus())
                .ge(ObjectUtils.isNotEmpty(query.getBeginTime()), InteractionQuestion::getCreateTime, query.getBeginTime())
                .le(ObjectUtils.isNotEmpty(query.getEndTime()), InteractionQuestion::getCreateTime, query.getEndTime())
                .page(query.toMpPageDefaultSortByCreateTimeDesc());
        List<InteractionQuestion> records = pageResult.getRecords();
        if (ObjectUtils.isEmpty(records)) {
            return PageDTO.empty(pageResult);
        }

        // 3、补充信息查询（章节数据、课程数据、用户数据）
        // 3.0、收集各种业务ID
        Set<Long> userIds = new HashSet<>(records.size());   //  用户ID 集合
        Set<Long> cIds = new HashSet<>(records.size());      //  课程ID 集合
        Set<Long> cataIds = new HashSet<>(records.size());   //  章节ID 集合
        records.forEach(r -> {
            userIds.add(r.getUserId());
            cIds.add(r.getCourseId());

            cataIds.add(r.getChapterId());
            cataIds.add(r.getSectionId());
        });

        // 3.1、根据用户ID集合查询用户数据
        Map<Long, String> userInfoMap = new HashMap<>(userIds.size());
        List<UserDTO> userInfoList = userClient.queryUserByIds(userIds);
        if (ObjectUtils.isNotEmpty(userInfoList)) {
            userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserDTO::getId, UserDTO::getName));
        }

        // 3.2、根据课程ID集合查询课程数据
        Map<Long, CourseSimpleInfoDTO> courseInfoMap = new HashMap<>(cIds.size());
        List<CourseSimpleInfoDTO> courseInfoList = courseClient.getSimpleInfoList(cIds);
        if (ObjectUtils.isNotEmpty(courseInfoList)) {
            courseInfoMap = courseInfoList.stream().collect(Collectors.toMap(CourseSimpleInfoDTO::getId, Function.identity()));
        }

        // 3.3、根据章节ID集合查询章节数据
        Map<Long, String> cataInfoMap = new HashMap<>(cataIds.size());
        List<CataSimpleInfoDTO> cataInfoList = catalogueClient.batchQueryCatalogue(cataIds);
        if (ObjectUtils.isNotEmpty(cataInfoList)) {
            cataInfoMap = cataInfoList.stream().collect(Collectors.toMap(CataSimpleInfoDTO::getId, CataSimpleInfoDTO::getName));
        }

        // 4、循环列表，填充各种数据
        List<QuestionAdminVO> voList = new ArrayList<>(records.size());
        for (InteractionQuestion record : records) {
            // 4.1、属性拷贝，po -> vo
            QuestionAdminVO adminVO = BeanUtils.copyBean(record, QuestionAdminVO.class);

            // 4.2、填充用户昵称
            adminVO.setUserName(userInfoMap.getOrDefault(record.getUserId(), ""));

            // 4.3、填充课程名称 和 三级分类信息
            CourseSimpleInfoDTO courseInfo = courseInfoMap.get(record.getCourseId());
            if (ObjectUtils.isNotEmpty(courseInfo)) {
                adminVO.setCourseName(courseInfo.getName());
                // 已知： 三级分类ID集合  ——> 三级分类名称拼接字符串
                adminVO.setCategoryName(categoryCache.getCategoryNames(courseInfo.getCategoryIds()));
            }

            // 4.4、填充章节名称
            adminVO.setChapterName(cataInfoMap.getOrDefault(record.getChapterId(), ""));
            adminVO.setSectionName(cataInfoMap.getOrDefault(record.getSectionId(), ""));

            // 4.5、添加·vo至volist
            voList.add(adminVO);
        }

        // 5、封装并返回PageDTO
        return PageDTO.of(pageResult, voList);
    }


    /**
     * 5、管理端-根据ID查询问题详情（Day5 - 随堂4）
     *
     * @param id
     * @return
     */
    @Override
    public QuestionAdminVO queryQuestionByIdAdmin(Long id) {
        // 1.根据id查询问题
        InteractionQuestion question = getById(id);
        if (question == null) {
            return null;
        }
        // 2.转PO为VO
        QuestionAdminVO vo = BeanUtils.copyBean(question, QuestionAdminVO.class);
        // 3.查询提问者信息
        UserDTO user = userClient.queryUserById(question.getUserId());
        if (user != null) {
            vo.setUserName(user.getName());
            vo.setUserIcon(user.getIcon());
        }
        // 4.查询课程信息
        CourseFullInfoDTO cInfo = courseClient.getCourseInfoById(
                question.getCourseId(), false, true);
        if (cInfo != null) {
            // 4.1.课程名称信息
            vo.setCourseName(cInfo.getName());
            // 4.2.分类信息
            vo.setCategoryName(categoryCache.getCategoryNames(cInfo.getCategoryIds()));
            // 4.3.教师信息
            List<Long> teacherIds = cInfo.getTeacherIds();
            List<UserDTO> teachers = userClient.queryUserByIds(teacherIds);
            if (ObjectUtil.isNotEmpty(teachers)) {
                vo.setTeacherName(teachers.stream()
                        .map(UserDTO::getName).collect(Collectors.joining("/")));
            }
        }
        // 5.查询章节信息
        List<CataSimpleInfoDTO> catas = catalogueClient.batchQueryCatalogue(
                List.of(question.getChapterId(), question.getSectionId()));
        Map<Long, String> cataMap = new HashMap<>(catas.size());
        if (ObjectUtil.isNotEmpty(catas)) {
            cataMap = catas.stream().collect(Collectors.toMap(CataSimpleInfoDTO::getId, CataSimpleInfoDTO::getName));
        }
        vo.setChapterName(cataMap.getOrDefault(question.getChapterId(), ""));
        vo.setSectionName(cataMap.getOrDefault(question.getSectionId(), ""));
        // 6.封装VO
        return vo;
    }


    /**
     * 6.根據ID删除问题
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        // 1.获取当前登录用户
        Long userId = UserContext.getUser();
        // 2.查询当前问题
        InteractionQuestion q = getById(id);
        if (q == null) {
            return;
        }
        // 3.判断是否是当前用户的问题
        if (!q.getUserId().equals(userId)) {
            // 不是，抛出异常
            throw new BadRequestException("无权删除他人的问题");
        }
        // 4.删除问题
        removeById(id);
        // 5.删除回答
        replyMapper.delete(
                new QueryWrapper<InteractionReply>().lambda().eq(InteractionReply::getQuestionId, id)
        );
    }


    /**
     * 7.根據ID修改问题
     *
     * @param id
     * @param questionDTO
     */
    @Override
    public void updateQuestion(Long id, QuestionFormDTO questionDTO) {
        // 1.获取当前登录用户
        Long userId = UserContext.getUser();
        // 2.查询当前问题
        InteractionQuestion q = getById(id);
        if (q == null) {
            throw new BadRequestException("问题不存在");
        }
        // 3.判断是否是当前用户的问题
        if (!q.getUserId().equals(userId)) {
            // 不是，抛出异常
            throw new BadRequestException("无权修改他人的问题");
        }
        // 4.修改问题
        InteractionQuestion question = BeanUtils.toBean(questionDTO, InteractionQuestion.class);
        question.setId(id);
        updateById(question);
    }


    /**
     * 8.修改问题的隐藏状态
     *
     * @param id
     * @param hidden
     */
    @Override
    public void hiddenQuestion(Long id, Boolean hidden) {
        // 1.直接更新问题
        InteractionQuestion question = new InteractionQuestion();
        question.setId(id);
        question.setHidden(hidden);
        updateById(question);
        // 2.没有answerId，说明自己是回答，需要隐藏回答下的评论
        replyMapper.update(null,
                Wrappers.<InteractionReply>lambdaUpdate()
                        .set(InteractionReply::getHidden, hidden)
                        .eq(InteractionReply::getQuestionId, id)
        );
    }
}
